package org.dromara.system.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.dromara.system.domain.WbProductPatent;
import org.springframework.stereotype.Service;
import org.dromara.system.domain.bo.WbCompanyProductListBo;
import org.dromara.system.domain.vo.WbCompanyProductListVo;
import org.dromara.system.domain.WbCompanyProductList;
import org.dromara.system.mapper.WbCompanyProductListMapper;
import org.dromara.system.service.IWbCompanyProductListService;

import java.util.List;
import java.util.Map;
import java.util.Collection;
import java.util.stream.Collectors;

import static cn.hutool.core.util.ArrayUtil.distinct;

/**
 * 各公司产品信息Service业务层处理
 *
 * @author Lion Li
 * @date 2024-04-08
 */
@RequiredArgsConstructor
@Service
public class WbCompanyProductListServiceImpl implements IWbCompanyProductListService {

    private final WbCompanyProductListMapper baseMapper;

    /**
     * 查询各公司产品信息
     */
    @Override
    public WbCompanyProductListVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询各公司产品信息列表
     */
    @Override
    public TableDataInfo<WbCompanyProductListVo> queryPageList(WbCompanyProductListBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<WbCompanyProductList> lqw = buildQueryWrapper(bo);
        lqw.groupBy(WbCompanyProductList::getCompanyName,WbCompanyProductList::getProductName);
        Page<WbCompanyProductListVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询各公司产品信息列表
     */
    @Override
    public List<WbCompanyProductListVo> queryList(WbCompanyProductListBo bo) {
        LambdaQueryWrapper<WbCompanyProductList> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<WbCompanyProductList> buildQueryWrapper(WbCompanyProductListBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<WbCompanyProductList> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getCompanyId() != null, WbCompanyProductList::getCompanyId, bo.getCompanyId());
        lqw.like(StringUtils.isNotBlank(bo.getCompanyName()), WbCompanyProductList::getCompanyName, bo.getCompanyName());
        lqw.like(StringUtils.isNotBlank(bo.getProductName()), WbCompanyProductList::getProductName, bo.getProductName());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), WbCompanyProductList::getStatus, bo.getStatus());
        return lqw;
    }

    /**
     * 新增各公司产品信息
     */
    @Override
    public Boolean insertByBo(WbCompanyProductListBo bo) {
        WbCompanyProductList add = MapstructUtils.convert(bo, WbCompanyProductList.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改各公司产品信息
     */
    @Override
    public Boolean updateByBo(WbCompanyProductListBo bo) {
        WbCompanyProductList update = MapstructUtils.convert(bo, WbCompanyProductList.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(WbCompanyProductList entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除各公司产品信息
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public TableDataInfo queryListSift(WbCompanyProductListBo bo, PageQuery pageQuery) {
        if (bo.getProductName()!=null && bo.getProductName().equals("全部产品")){
            bo.setProductName("");
        }
        List<String> productNames=baseMapper.queryListSift(bo,pageQuery.build());
        return TableDataInfo.build(productNames);
    }

    @Override
    public TableDataInfo<WbCompanyProductListVo> queryPageListZ(WbCompanyProductListBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<WbCompanyProductList> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getCompanyId() != null, WbCompanyProductList::getCompanyId, bo.getCompanyId());
        Page<WbCompanyProductListVo> result = baseMapper.selectVoPageZ(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }
}
